﻿#region
/* 
 * Descripciòn: Clase que representa el mapeo de un area.
 * Programador: Marco Torres
 * Fecha: 14 Mayo 2012
 * 
 * Log de modificaciones.
 * Descripciòn:
 * Programador:
 * Fecha:
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using System.Data;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.Logging;

namespace Demo.Services.Integration.DAO.IntegrationComponents.Mappings
{
    public class AreaDAOMapper
    {
        public static AreaResultInfo GetAreas(DataSet ds)
        {
            AreaResultInfo result;
            List<AreaInfo> areaList;
            try
            {
                Logger.Info(MethodInfo.GetCurrentMethod().ToString());
                DataTable dt = ds.Tables[1];

                areaList = (from area in dt.AsEnumerable()
                                 select new AreaInfo
                                 {
                                     Id = area.Field<int>("Id"),
                                     Description = area.Field<string>("Nombre")
                                 }).ToList();
                result = new AreaResultInfo();
                result.AreaList = areaList;
                result.TotalReg = Convert.ToInt32(ds.Tables[0].Rows[0]["TotalReg"]);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }
       
        public static List<AreaInfo> GetAreaList(DataSet ds)
        {
            List<AreaInfo> result;
            try
            {
                Logger.Info(MethodInfo.GetCurrentMethod().ToString());
                DataTable dt = ds.Tables[0];

                result = (from area in dt.AsEnumerable()
                          select new AreaInfo
                          {
                              Id = area.Field<int>("id"),
                              Description = area.Field<string>("nombre")
                          }).ToList();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }

        /// <summary>
        /// Obtener un area.
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static AreaInfo GetAreaOne(DataSet ds)
        {
            AreaInfo result;
            try
            {
                Logger.Info(MethodInfo.GetCurrentMethod().ToString());
                DataTable dt = ds.Tables[0];

                result = (from area in dt.AsEnumerable()
                          select new AreaInfo
                          {
                              Id = area.Field<int>("id"),
                              Description = area.Field<string>("nombre")
                          }).ToList()[0];
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

            return result;
        }
    }
}
